<template>
  <view class="popular-services">
    <!-- 标题区域 -->
    <view class="services-header">
      <view class="header-content">
        <text class="title-text">热门服务</text>
        <text class="subtitle-text">专业服务 · 品质保障</text>
      </view>
      <view class="header-decoration">
        <view class="decoration-circle"></view>
        <view class="decoration-circle"></view>
      </view>
    </view>

    <!-- 服务卡片容器 -->
    <view class="services-container">
      <!-- 第一行：助医中心和助行中心 -->
      <view class="services-row">
        <view class="service-card medical-card" @click="handleServiceClick('medical')">
          <view class="card-content">
            <view class="card-text">
              <text class="card-title">助医中心</text>
              <text class="card-desc">专业医疗咨询与协助</text>
            </view>
            <view class="card-icon">🏥</view>
          </view>
        </view>

        <view class="service-card mobility-card" @click="handleServiceClick('mobility')">
          <view class="card-content">
            <view class="card-text">
              <text class="card-title">助行中心</text>
              <text class="card-desc">出行辅助与交通支持</text>
            </view>
            <view class="card-icon">♿</view>
          </view>
        </view>
      </view>

      <!-- 中间大卡片：个案管理中心 -->
      <view class="service-card-large case-card" @click="handleServiceClick('case')">
        <view class="card-content">
          <view class="card-main">
            <view class="card-text">
              <text class="card-title">个案管理中心</text>
              <text class="card-desc">个性化服务方案定制</text>
            </view>
            <view class="card-icon">📋</view>
          </view>
          <view class="card-badge-large">
            <text class="badge-text">热门推荐</text>
          </view>
        </view>
      </view>

      <!-- 第三行：母婴中心和老年中心 -->
      <view class="services-row">
        <view class="service-card maternal-card" @click="handleServiceClick('maternal')">
          <view class="card-content">
            <view class="card-text">
              <text class="card-title">母婴中心</text>
              <text class="card-desc">孕产护理与婴幼儿照护</text>
            </view>
            <view class="card-icon">👶</view>
          </view>
        </view>

        <view class="service-card elderly-card" @click="handleServiceClick('elderly')">
          <view class="card-content">
            <view class="card-text">
              <text class="card-title">老年中心</text>
              <text class="card-desc">老年人照护与健康管理</text>
            </view>
            <view class="card-icon">👴</view>
          </view>
        </view>
      </view>
    </view>

    <!-- 底部弹出模态框 -->
    <view v-if="showModal" class="modal-overlay" @click="closeModal">
      <view class="modal-content" @click.stop>
        <view class="modal-header">
          <text class="modal-title">{{ currentService?.name }}</text>
          <view class="modal-close" @click="closeModal">
            <text class="close-icon">×</text>
          </view>
        </view>
        
        <view class="modal-body">
          <view class="service-list">
            <view 
              v-for="(item, index) in getServiceList(currentService?.type)" 
              :key="index" 
              class="service-item"
              @click="handleItemClick(item)"
            >
              <view class="item-image">
                <image :src="item.image" class="service-image" mode="aspectFill" />
              </view>
              <view class="item-content">
                <text class="item-title">{{ item.title }}</text>
                <text class="item-desc">{{ item.description }}</text>
              </view>
              <view class="item-arrow">
                <text class="arrow-icon">></text>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
/**
 * 热门服务组件 - 优化版
 * 展示5个服务卡片，包含助医中心、助行中心、个案管理中心、母婴中心、老年中心
 * @description 优化后的设计更加大气、现代和专业，支持底部弹出模态框
 */

import { ref } from 'vue'

interface ServiceType {
  medical: string
  mobility: string
  case: string
  maternal: string
  elderly: string
}

interface ServiceItem {
  title: string
  description: string
  image: string
  id: string
}

interface CurrentService {
  type: keyof ServiceType
  name: string
}

// 服务类型映射
const serviceMap: ServiceType = {
  medical: '助医中心',
  mobility: '助行中心',
  case: '个案管理中心',
  maternal: '母婴中心',
  elderly: '老年中心'
}

// 响应式数据
const showModal = ref(false)
const currentService = ref<CurrentService | null>(null)

// 服务详情数据
const serviceData = {
  medical: [
    {
      id: 'medical-1',
      title: '在线问诊',
      description: '专业医生在线咨询，快速解答健康问题',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    },
    {
      id: 'medical-2',
      title: '健康体检',
      description: '全面体检套餐，及时发现健康隐患',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    },
    {
      id: 'medical-3',
      title: '药品配送',
      description: '专业药品配送，安全便捷购药',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    },
    {
      id: 'medical-4',
      title: '急救服务',
      description: '24小时急救热线，紧急情况快速响应',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    }
  ],
  mobility: [
    {
      id: 'mobility-1',
      title: '轮椅租赁',
      description: '高品质轮椅租赁，临时出行无忧',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    },
    {
      id: 'mobility-2',
      title: '无障碍出行',
      description: '专业无障碍出行规划，贴心服务',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    },
    {
      id: 'mobility-3',
      title: '康复训练',
      description: '专业康复训练指导，恢复行动能力',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    }
  ],
  case: [
    {
      id: 'case-1',
      title: '健康评估',
      description: '全面健康状态评估，制定个性化方案',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    },
    {
      id: 'case-2',
      title: '照护计划',
      description: '专业照护计划制定，科学管理健康',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    },
    {
      id: 'case-3',
      title: '跟踪服务',
      description: '全程跟踪服务，及时调整照护方案',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    },
    {
      id: 'case-4',
      title: '家庭支持',
      description: '家庭照护指导，提升照护质量',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    }
  ],
  maternal: [
    {
      id: 'maternal-1',
      title: '孕期护理',
      description: '专业孕期护理指导，安心度过孕期',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    },
    {
      id: 'maternal-2',
      title: '产后恢复',
      description: '科学产后恢复方案，快速恢复身体',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    },
    {
      id: 'maternal-3',
      title: '婴幼儿护理',
      description: '专业婴幼儿护理知识，科学育儿',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    }
  ],
  elderly: [
    {
      id: 'elderly-1',
      title: '健康监测',
      description: '24小时健康监测，及时发现异常',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    },
    {
      id: 'elderly-2',
      title: '生活照料',
      description: '日常生活照料，贴心细致服务',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    },
    {
      id: 'elderly-3',
      title: '心理关怀',
      description: '专业心理关怀，关注精神健康',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    },
    {
      id: 'elderly-4',
      title: '康复理疗',
      description: '专业康复理疗，延缓身体机能衰退',
      image: 'http://www.dzgktf.com/templets/default/images/wtyt.jpg'
    }
  ]
}

/**
 * 处理服务卡片点击事件
 * @param serviceType - 服务类型
 */
const handleServiceClick = (serviceType: keyof ServiceType) => {
  const serviceName = serviceMap[serviceType]
  currentService.value = {
    type: serviceType,
    name: serviceName
  }
  showModal.value = true
  
  // 触发自定义事件，让父组件处理跳转逻辑
  emit('serviceClick', {
    type: serviceType,
    name: serviceName
  })
}

/**
 * 获取服务列表
 * @param type - 服务类型
 * @returns 服务列表
 */
const getServiceList = (type: keyof ServiceType | undefined): ServiceItem[] => {
  if (!type || !serviceData[type]) {
    return []
  }
  return serviceData[type]
}

/**
 * 处理服务项目点击
 * @param item - 服务项目
 */
const handleItemClick = (item: ServiceItem) => {
  console.log('点击服务项目:', item)
  // 这里可以添加具体的跳转逻辑或API调用
  // uni.showToast({
  //   title: `点击了${item.title}`,
  //   icon: 'success',
  //   duration: 1500
  // })
}

/**
 * 关闭模态框
 */
const closeModal = () => {
  showModal.value = false
  currentService.value = null
}

// 定义事件
const emit = defineEmits<{
  serviceClick: [payload: { type: keyof ServiceType; name: string }]
}>()
</script>

<style lang="scss" scoped>
.popular-services {
  width: 100%;
  padding: 32rpx 24rpx;
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
  min-height: 100vh;
  box-sizing: border-box;
}

// 标题区域
.services-header {
  width: 100%;
  margin-bottom: 48rpx;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  box-sizing: border-box;
  
  .header-content {
    flex: 1;
    
    .title-text {
      display: block;
      font-size: 52rpx;
      font-weight: 700;
      color: #1e293b;
      margin-bottom: 8rpx;
      line-height: 1.2;
    }
    
    .subtitle-text {
      font-size: 26rpx;
      color: #64748b;
      font-weight: 400;
      line-height: 1.4;
    }
  }
  
  .header-decoration {
    display: flex;
    flex-direction: column;
    gap: 8rpx;
    
    .decoration-circle {
      width: 20rpx;
      height: 20rpx;
      border-radius: 50%;
      background: linear-gradient(135deg, #3b82f6, #8b5cf6);
      
      &:last-child {
        background: linear-gradient(135deg, #8b5cf6, #ec4899);
      }
    }
  }
}

.services-container {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 24rpx;
  box-sizing: border-box;
}

.services-row {
  width: 100%;
  display: flex;
  gap: 20rpx;
  box-sizing: border-box;
}

// 通用卡片样式
.service-card {
  flex: 1;
  height: 140rpx;
  border-radius: 16rpx;
  position: relative;
  overflow: hidden;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  box-sizing: border-box;
  
  &:active {
    transform: scale(0.98);
  }
  
  .card-content {
    width: 100%;
    height: 100%;
    padding: 20rpx 24rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;
  }
  
  .card-text {
    flex: 1;
    min-width: 0;
    
    .card-title {
      display: block;
      font-size: 30rpx;
      font-weight: 600;
      color: #1e293b;
      margin-bottom: 4rpx;
      line-height: 1.2;
    }
    
    .card-desc {
      display: block;
      font-size: 22rpx;
      color: #64748b;
      font-weight: 400;
      line-height: 1.3;
    }
  }
  
  .card-icon {
    font-size: 48rpx;
    line-height: 1;
    flex-shrink: 0;
    margin-left: 16rpx;
  }
}

// 大卡片样式
.service-card-large {
  width: 100%;
  height: 160rpx;
  border-radius: 16rpx;
  position: relative;
  overflow: hidden;
  box-shadow: 0 6rpx 24rpx rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  box-sizing: border-box;
  
  &:active {
    transform: scale(0.98);
  }
  
  .card-content {
    width: 100%;
    height: 100%;
    padding: 20rpx 24rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;
  }
  
  .card-main {
    display: flex;
    align-items: center;
    flex: 1;
    min-width: 0;
  }
  
  .card-text {
    flex: 1;
    min-width: 0;
    
    .card-title {
      display: block;
      font-size: 34rpx;
      font-weight: 700;
      color: #1e293b;
      margin-bottom: 4rpx;
      line-height: 1.2;
    }
    
    .card-desc {
      display: block;
      font-size: 24rpx;
      color: #64748b;
      font-weight: 400;
      line-height: 1.3;
    }
  }
  
  .card-icon {
    font-size: 56rpx;
    line-height: 1;
    flex-shrink: 0;
    margin: 0 20rpx;
  }
  
  .card-badge-large {
    background: linear-gradient(135deg, #f59e0b, #ec4899);
    padding: 6rpx 14rpx;
    border-radius: 14rpx;
    box-shadow: 0 3rpx 10rpx rgba(245, 158, 11, 0.3);
    flex-shrink: 0;
    
    .badge-text {
      font-size: 18rpx;
      font-weight: 600;
      color: #fff;
      white-space: nowrap;
    }
  }
}

// 各卡片颜色主题
.medical-card {
  background: linear-gradient(135deg, #dbeafe, #eff6ff);
}

.mobility-card {
  background: linear-gradient(135deg, #e0e7ff, #ede9fe);
}

.case-card {
  background: linear-gradient(135deg, #fef3c7, #fef7ed);
}

.maternal-card {
  background: linear-gradient(135deg, #fce7f3, #fdf2f8);
}

.elderly-card {
  background: linear-gradient(135deg, #dcfce7, #f0fdf4);
}

// 模态框样式
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(8rpx);
  animation: fadeIn 0.3s ease;
}

.modal-content {
  width: 100%;
  max-height: 80vh;
  background: #fff;
  border-radius: 24rpx 24rpx 0 0;
  overflow: hidden;
  animation: slideUp 0.3s ease;
  box-shadow: 0 -8rpx 32rpx rgba(0, 0, 0, 0.15);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 32rpx 32rpx 24rpx;
  border-bottom: 1rpx solid #f1f5f9;
  
  .modal-title {
    font-size: 36rpx;
    font-weight: 700;
    color: #1e293b;
  }
  
  .modal-close {
    width: 48rpx;
    height: 48rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: #f1f5f9;
    
    .close-icon {
      font-size: 32rpx;
      color: #64748b;
      font-weight: 300;
    }
  }
}

.modal-body {
  padding: 24rpx 32rpx 40rpx;
  max-height: calc(80vh - 120rpx);
  overflow-y: auto;
}

.service-list {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}

.service-item {
  display: flex;
  align-items: center;
  padding: 24rpx;
  background: #f8fafc;
  border-radius: 16rpx;
  transition: all 0.3s ease;
  
  &:active {
    background: #f1f5f9;
    transform: scale(0.98);
  }
}

.item-image {
  width: 80rpx;
  height: 80rpx;
  border-radius: 12rpx;
  overflow: hidden;
  margin-right: 20rpx;
  flex-shrink: 0;
  
  .service-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}

.item-content {
  flex: 1;
  min-width: 0;
  
  .item-title {
    display: block;
    font-size: 28rpx;
    font-weight: 600;
    color: #1e293b;
    margin-bottom: 6rpx;
    line-height: 1.3;
  }
  
  .item-desc {
    display: block;
    font-size: 24rpx;
    color: #64748b;
    line-height: 1.4;
  }
}

.item-arrow {
  width: 32rpx;
  height: 32rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 16rpx;
  flex-shrink: 0;
  
  .arrow-icon {
    font-size: 24rpx;
    color: #94a3b8;
    font-weight: 300;
  }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

</style>